<template>
  <div class="login">
    <el-form ref="form" :model="loginData" :rules="rules" label-width="70px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginData.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginData.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login"> 登录 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { employeeStore } from '@/stores/index.js'
import { useRouter } from 'vue-router'
import { ref, reactive } from 'vue'
const store = employeeStore()
const router = useRouter()
const loginData = reactive({
  username: 'admin',
  password: '123456'
})
const form = ref()
const rules = {
  username: [{ required: true, message: '用户名必填', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const login = () => {
  form.value.validate(async (isOk) => {
    if (!isOk) return
    await store.setToken(loginData)
    router.push('/')
  })
}
</script>

<style lang="scss" scoped>
.login {
  width: 300px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
